package com.jc.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jc.auth.application.convert.AuthPermissionDTOConverter;
import com.jc.auth.application.convert.AuthRoleDTOConverter;
import com.jc.auth.application.dto.AuthPermissionDTO;
import com.jc.auth.application.dto.AuthRoleDTO;
import com.jc.auth.common.entity.Result;
import com.jc.auth.domain.entity.AuthPermissionBO;
import com.jc.auth.domain.entity.AuthRoleBO;
import com.jc.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.auth.application.controller
 * @Project: jc-club
 * @Date: 2024/10/29  13:07
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("permissionDTOController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"权限父id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()),"权限key不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter
                    .INSTANCE.convertPermissionDTOToBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.add(authPermissionBO);
            return com.jc.auth.common.entity.Result.success(result);
        } catch (Exception e) {
            log.error("permissionController.add.dto:{}", e.getMessage(),e);
            return Result.fail("新增权限失败");
        }
    }
    @RequestMapping("/getPermission")
    public Result<List<String>> getPermission(String userName){
        try {
            if(log.isInfoEnabled()){
                log.info("permissionDTOController.getPermission.userName:{}", userName);
            }
            Preconditions.checkArgument(!StringUtils.isBlank(userName),"用户名不能为空");
            return Result.success(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("permissionController.getPermission:{}", e.getMessage(),e);
            return Result.fail("查询权限失败");
        }
    }

    /**
     * 修改权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("permissionDTOController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter
                    .INSTANCE.convertPermissionDTOToBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.update(authPermissionBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("permissionController.update.dto:{}", e.getMessage(),e);
            return Result.fail("修改权限失败");
        }
    }

    /**
     * 删除权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("permissionDTOController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter
                    .INSTANCE.convertPermissionDTOToBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.delete(authPermissionBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("permissionController.delete.dto:{}", e.getMessage(),e);
            return Result.fail("删除权限失败");
        }
    }
}
